package com.ylkj.patientdrug.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ylkj.patientdrug.entity.DrugEntity;
import com.ylkj.patientdrug.entity.MessageEvent;
import com.ylkj.patientdrug.R;
import com.ylkj.patientdrug.entity.O2oCartVOSEntity;
import com.ylkj.patientdrug.view.SwipeItemLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import static com.examination.mlib.constants.AllStringConstants.*;


public class DrugAdapter extends BaseMultiItemQuickAdapter<DrugEntity, BaseViewHolder> {


    private List<DrugEntity> labelList = new ArrayList<>();
    private DrugInfoAdapter drugInfoAdapter;

    public DrugAdapter(@Nullable List<DrugEntity> data) {
        super(data);
        addItemType(ITEM_TYPE_DRUG, R.layout.item_drug);
        addItemType(ITEM_TYPE_O2O_CART_VOS, R.layout.item_drug);
    }

    @Override
    protected void convert(BaseViewHolder viewHolder, DrugEntity item) {
        RecyclerView drugRv = viewHolder.getView(R.id.drug_info_list);
        int commodityType = 0;
        if (viewHolder.getItemViewType() == ITEM_TYPE_DRUG) {
            commodityType = ITEM_TYPE_DRUG_COMMODITY;
            viewHolder.setVisible(R.id.tv_tag, false);
        } else if (viewHolder.getItemViewType() == ITEM_TYPE_O2O_CART_VOS) {
            commodityType = ITEM_TYPE_O2O_CART_VOS_COMMODITY;
            viewHolder.setVisible(R.id.tv_tag, true);
        }
        drugInfoAdapter = new DrugInfoAdapter(R.layout.item_drug_info, item.getCommodityList(), commodityType, item);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setInitialPrefetchItemCount(4);
        drugRv.setLayoutManager(manager);
//        drugRv.addOnItemTouchListener(new SwipeItemLayout.OnSwipeItemTouchListener(mContext));
        drugRv.setAdapter(drugInfoAdapter);
        drugInfoAdapter.setPosition(viewHolder.getAdapterPosition());
//        viewHolder.setAdapter(R.id.drug_info_list, (Adapter) drugInfoAdapter);
        if (!TextUtils.isEmpty(item.getMarketDefaultDoctorName())) {
            viewHolder.setText(R.id.item_drug_doctorname, item.getMarketDefaultDoctorName());
        } else {
            if (viewHolder.getItemViewType() == ITEM_TYPE_DRUG) {
                viewHolder.setText(R.id.item_drug_doctorname, TextUtils.isEmpty(item.getName()) ? "医生的问诊开方服务" : (item.getName() + "医生的问诊开方服务"));
            } else if (viewHolder.getItemViewType() == ITEM_TYPE_O2O_CART_VOS) {
//            viewHolder.setText(R.id.item_drug_doctorname, ((O2oCartVOSEntity) item).getStoreName() + "医生的问诊开方服务");
                viewHolder.setText(R.id.item_drug_doctorname, ((O2oCartVOSEntity) item).getStoreName());
            }
        }
        drugInfoAdapter.setOnDeleteClickListener(new DrugInfoAdapter.OnDeleteClickLister() {
            @Override
            public void onDeleteClick(View view, int position) {
                MessageEvent messageEvent = new MessageEvent("delete");
                messageEvent.setPosition(viewHolder.getAdapterPosition());
                messageEvent.setChildPosition(position);
                EventBus.getDefault().post(messageEvent);
            }
        });

        //控制选中按钮
        if (item.isChoice()) {
            viewHolder.setBackgroundRes(R.id.choice_iv, R.mipmap.checked);
        } else {
            viewHolder.setBackgroundRes(R.id.choice_iv, R.mipmap.uncheck);
        }
        viewHolder.setOnClickListener(R.id.choice_iv, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if (DrugListUtils.isFastClick()){
                MessageEvent messageEvent = new MessageEvent("allChoice");
                messageEvent.setAllChoice(!item.isChoice());
                messageEvent.setPosition(viewHolder.getPosition());
                EventBus.getDefault().post(messageEvent);
//                }
            }
        });
    }

}
